/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menus;

import com.sun.lwuit.Form;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import util.Menu;

/**
 *
 * @author nialldeasy
 */
public class Landmarks extends Menu {
    Vector vlandmarks = null;//<string name, Vector<String,Double[]>>
    Vector vNames = null;//String of cities for in
    Vector landmarks = null;
    String location = "";
    List main=null;
    protected void execute(Form f) {
        
        this.setBackround("notepad.jpg");
        routine();
        this.setLayout(4);
        main = new List(vectorToString(vNames));
        main.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
              //  f.removeComponent(main);
                System.out.println(main.getSelectedItem().toString());
                open(main.getSelectedItem().toString());
            }
        });
        f.addComponent(main);
    }
    public void open(String location){
        f.removeComponent(main);
        final List L = new List(get(location));
        L.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {

                   build_me_a_reader = true;
                    reader = L.getSelectedItem().toString();
            }
        });
        f.addComponent(L);

    }
    public void recycleBin() {
        landmarks = null;
        vNames=null;
        vlandmarks=null;
    }

    public void update() {
    }

    public double[] get_position(String landmark) {
        if (vlandmarks==null) {
            routine();
        }

        double[] pos = new double[2];
        for(int I = 0; I<vlandmarks.size();I++){
            Vector Landmarks = (Vector) vlandmarks.elementAt(I);
        for (int i = 0; i < Landmarks.size(); i++) {
            Vector stringdoublearray = (Vector) Landmarks.elementAt(i);
            String name = (String) stringdoublearray.elementAt(0);
            pos = (double[]) stringdoublearray.elementAt(1);
            if (name.equalsIgnoreCase(landmark)) {
                System.out.println(name+pos);
                return pos;
            }
        }
        }
        return null;
    }
    public String[] vectorToString(Vector ofStrings){
        String[] list = new String[ofStrings.size()];
        for (int i = 0; i < ofStrings.size(); i++) {
            list[i] = ((String) (ofStrings.elementAt(i)));
        }
        return list;
    }
    public String[] get(String s){
        int index=-1;
        for(int i=0;i<vNames.size();i++){

            String curr = (String)vNames.elementAt(i);
            if(curr.equalsIgnoreCase(s)){
                index=i;
            }
        }
        if(index!=-1){
        Vector Landmarks = (Vector) vlandmarks.elementAt(index);
        String[] list = new String[Landmarks.size()];
        for (int i = 0; i < Landmarks.size(); i++) {
            list[i] = ((String) ((Vector) Landmarks.elementAt(i)).elementAt(0));
        }
        
        return list;
        }
        return null;
    }
//    public String[] getList() {
//        int index=-1;
//        for(int i=0;i<vNames.size();i++){
//            String curr = (String)vNames.elementAt(i);
//            if(curr.equalsIgnoreCase(Name)){
//                index=i;
//            }
//        }
//
//        String[] list = new String[landmarks.size()];
//        for (int i = 0; i < landmarks.size(); i++) {
//            list[i] = ((String) ((Vector) landmarks.elementAt(i)).elementAt(0));
//        }
//        return list;
//    }

    public String readFile() {
     //   OutputStream os = new OutputStream() {};

        InputStream is = getClass().getResourceAsStream("/Landmarks/landmarks.txt");
        StringBuffer sb = new StringBuffer();
        try {
            int chars, i = 0;
            while ((chars = is.read()) != -1) {

                sb.append((char) chars);
            }
            return sb.toString();
        } catch (Exception e) {
        }
        return null;
    }

    public void routine() {
        vlandmarks = new Vector();
        vNames = new Vector();
        landmarks = new Vector();
        location = "";
        String read = readFile();
        String name = "";
        int s = -1;
        double[] pos = new double[2];
        String lon = "";
        String lat = "";
        for (int i = 0; i < read.toCharArray().length; i++) {
            switch (read.charAt(i)) {
                case '{':
                    location = "";
                    if(!landmarks.isEmpty()){vlandmarks.addElement(landmarks);}
                    landmarks=new Vector();
                    s = -1;
                    break;
                case '}':
                    vNames.addElement(location);
                    s = 0;
                    break;
                case '$':
                    vlandmarks.addElement(landmarks);
                    i = read.toCharArray().length;
                    return;
                case '<':
                    s = 0;
                    name = "";
                    lat = "";
                    lon = "";
                    break;
                case '>':
                    s = 1;
                    break;
                case '[':
                    pos = new double[2];
                    lat="";
                    lon="";
                    s = 1;
                    break;
                case ']':
                    pos[0] = Double.parseDouble(lon);
                    pos[1] = Double.parseDouble(lat);
                    add(name, pos);
                    break;
                case ',':
                    s = 2;
                    break;
                default:
                    switch (s) {
                        case -1:
                            location += read.charAt(i);
                            break;
                        case 0:
                            name += read.charAt(i);
                            break;
                        case 1:
                            lon += read.charAt(i);
                            break;
                        case 2:
                            lat += read.charAt(i);
                            break;
                    }
                    break;
            }
        }
    }

    public void add(String name, double[] pos) {
        Vector stringdoublearray = new Vector();
        stringdoublearray.addElement(name);
        stringdoublearray.addElement(pos);
        landmarks.addElement(stringdoublearray);
    }
}
